package com.travel.controller.user;

import com.travel.constant.ApiConstants;
import com.travel.model.dto.comment.CommentRequest;
import com.travel.model.dto.common.ApiResponse;
import com.travel.model.dto.post.PostDTO;
import com.travel.model.entity.auth.User;
import com.travel.model.entity.content.Comment;
import com.travel.model.entity.content.Post;
import com.travel.service.Service.PostService;
import com.travel.service.Service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
//用户文章接口 , 创建文章, 更新文章, 删除文章, 获取我的文章
@Tag(name = "用户文章", description = "用户文章管理接口")
@RestController
@RequestMapping(ApiConstants.API_USER + "/posts")
@RequiredArgsConstructor
public class UserPostController {
    private final PostService postService;
    private final UserService userService;

    @Operation(summary = "创建文章")
    @PostMapping
    public ApiResponse<PostDTO> createPost(
            @Parameter(description = "文章信息，包含标题、封面图片、内容等") 
            @Valid @RequestBody PostDTO postDTO) {
        return ApiResponse.success(postService.createPost(postDTO));
    }

    @Operation(summary = "更新文章")
    @PutMapping("/{id}")
    public ApiResponse<PostDTO> updatePost(
            @Parameter(description = "文章ID") 
            @PathVariable Long id,
            @Parameter(description = "更新的文章信息，包含标题、封面图片、内容等") 
            @Valid @RequestBody PostDTO postDTO) {
        return ApiResponse.success(postService.updatePost(id, postDTO));
    }

    @Operation(summary = "删除文章")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deletePost(
            @Parameter(description = "文章ID") 
            @PathVariable Long id) {
        postService.deletePost(id);
        return ApiResponse.success("删除成功");
    }

    @Operation(summary = "添加评论")
    @PostMapping("/{postId}/comments")
    public ApiResponse<Comment> addComment(
            @Parameter(description = "文章ID") 
            @PathVariable Long postId,
            @Valid @RequestBody CommentRequest request) {
        return ApiResponse.success(postService.addComment(postId, request));
    }

    @Operation(summary = "删除评论")
    @DeleteMapping("/comments/{commentId}")
    public ApiResponse<Void> deleteComment(
            @Parameter(description = "评论ID") 
            @PathVariable Long commentId) {
        User currentUser = userService.getCurrentUser();
        postService.deleteComment(commentId, currentUser.getId());
        return ApiResponse.success("删除成功");
    }

    @Operation(summary = "获取我的文章")
    @GetMapping("/my")
    public ApiResponse<Page<Post>> getMyPosts(Pageable pageable) {
        return ApiResponse.success(postService.getCurrentUserPosts(pageable));
    }
} 